package com.hnust.root.web.controller;

import cn.hutool.core.lang.Console;
import cn.hutool.http.HttpUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.hnust.root.common.base.response.BaseRestController;
import com.hnust.root.common.base.response.BaseResult;
import com.hnust.root.common.constant.ResultConstant;
import com.hnust.root.common.model.ManagerInfo;
import com.hnust.root.dao.entity.domain.Manager;
import com.hnust.root.service.auth.ManagerInfoService;
import com.hnust.root.service.auth.ManagerService;
import com.hnust.root.service.file.IOSSService;
import com.hnust.root.web.annotation.SysLog;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author zhusir
 * @Date 2019/1/26
 * @Desc
 */
@RequestMapping("/test")
@RestController
public class testController extends BaseRestController {

    private static final Log LOGGER = LogFactory.get();

    @Autowired
    private ManagerInfoService managerInfoService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ManagerService managerService;

    @Autowired
    private IOSSService iossService;

    @GetMapping("/hello")
    public BaseResult<String> helloworld() {
        return fillSuccessData("hello world!");
    }

    @GetMapping("/asyn")
    public BaseResult<String> testAsyn() {
        managerInfoService.testAysn();
        return fillSuccessData();
    }

    @GetMapping("/redis-cache")
    @SysLog(modelName = "测试模块",operation = "测试",desc = "测试从缓存中获取用户信息")
    public BaseResult<ManagerInfo> getManagerInfoFromCache() {
        ManagerInfo managerInfo = managerInfoService.findByUsername(getUserName());

        return fillSuccessData(managerInfo);
    }

    @GetMapping("/manager-id")
    @SysLog(modelName = "测试模块",operation = "测试",desc = "获取管理员信息")
    public BaseResult<Manager> getManageByuserName(@RequestParam("username") String username) {
        return fillSuccessData(managerService.getManageByUserName(username));
    }

    @ApiOperation(value = "上传文件到OSS", notes = "自用，非中车系统,目前bucket只能为 zhusir")
    @PostMapping("/oss_upload")
    public BaseResult<String> OSSupload(@RequestParam("file") MultipartFile file,
                                        @RequestParam(value = "category", required = false) String category) throws IOException {

        String path = iossService.upload(file, category);
        return path != null ? fillSuccessData(path) : fillFailData(ResultConstant.FILE_OP_FAIL);
    }

    @GetMapping("oss-download")
    @SysLog(modelName = "文件操作",operation = "文件下载",desc = "测试文件下载")
    public BaseResult<String> ossDownload(@RequestParam("fileUrl") String fileUrl,
                            @RequestParam("localPath") String locapath) {
        boolean result = iossService.downloadFile(fileUrl, locapath);
        if (result) {
            LOGGER.info("文件存储位置：" + locapath);
            return fillSuccessData(locapath);
        } else {
            return fillFailData(ResultConstant.FILE_OP_FAIL);
        }
    }


    public static String getAdd(String location ){
//        27.82975,112.94411
        //lat 小  log  大
        //参数解释: 纬度,经度 type 001 (100代表道路，010代表POI，001代表门址，111可以同时显示前三项)
        String urlString = "https://apis.map.qq.com/ws/geocoder/v1";
        Map<String, Object> params = new HashMap<>();
        params.put("location",location);
        params.put("key","ND3BZ-2PWWG-DSCQM-ISBW2-FEPHZ-KJFXF");
        params.put("output","json");

        String result = HttpUtil.get(urlString,params,5000);
        String city = result.split("\"city\": \"")[1].split("\"")[0];
        Console.log(city);
        return "";
    }

    public static void main(String[] args) {
        getAdd("27.82975,112.94411");
    }



}
